package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/findAll")

    public ResultInfo findAll(){
        List<Category> categories = categoryService.findAll();
        return ResultInfo.success(categories);
    }


    @PostMapping()
    public ResultInfo add(@RequestBody Category category){
        categoryService.add(category);
        return ResultInfo.success(null);
    }


    @PutMapping
    public ResultInfo update(@RequestBody Category category){
        categoryService.update(category);
        return ResultInfo.success(null);
    }
    @DeleteMapping
    public ResultInfo delete( Long id){
        categoryService.delete(id);
        return ResultInfo.success(null);
    }


    @GetMapping("/list")
    @Cacheable(value = "category",key = "categories")
    public ResultInfo findList(Integer type){
        //调用service进行查询口味列表
        List<Category> list = categoryService.findList(type);
        //返回
        return ResultInfo.success(list);
    }
}
